package com.ktwlsoft.fundmanage.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * DateUtils
 *
 * @author Name on 2019/4/16 11:21
 * @version 1.0
 */
public class DateUtils {

    /** 日志对象 **/
    private static Logger logger = LoggerFactory.getLogger(DateUtils.class);

    /**年月日,时分秒*/
    public static final String ALLSF_PATTERN = "yyyy-MM-dd HH:mm:ss";
    /**年月日,时分*/
    public static final String ALLSF_NO_SECEND_PATTERN = "yyyy-MM-dd HH:mm";
    /**年月日*/
    public static final String SIMPLESF_PATTERN = "yyyy-MM-dd";

    /**
     * 获取转换器 on 2019/4/16 11:31
     *
     * @param pattern pattern
     * @return SimpleDateFormat
     */
    private static SimpleDateFormat getSdf(String pattern){
        return new SimpleDateFormat(pattern);
    }

    /**
     * string转date on 2019/4/16 11:30
     *
     * @param str str
     * @return Date
     */
    public static Date strToDate(String str){
        Date date = null;
        try {
            int n = 10;
            if(str !=null && !"".equals(str)){
                if(str.length()>n){
                    date = getSdf(ALLSF_PATTERN).parse(str);
                } else {
                    date = getSdf(SIMPLESF_PATTERN).parse(str);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return date;
    }

    /**
     * date转string on 2019/4/16 11:30
     *
     * @param date date
     * @return String
     */
    public static String dateToStr(Date date,String dateType){
        String str = "";
        try {
            str = getSdf(dateType).format(date);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return str;
    }

    /**
     * date转string on 2019/4/16 11:30
     *
     * @param date date
     * @return String
     */
    public static String dateToStr(Date date){
        String str = "";
        try {
            str = getSdf(ALLSF_PATTERN).format(date);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return str;
    }

    /**
     * 两个时间相差的月数
     *
     * @param startDate startDate
     * @param endDate endDate
     * @return int
     */
    public static int getTowDateMonth(String startDate, String endDate){
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(getSdf(SIMPLESF_PATTERN).parse(startDate));
            c2.setTime(getSdf(SIMPLESF_PATTERN).parse(endDate));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        int result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        return result == 0 ? 1 : Math.abs(result);
    }

    /**
     * 两个时间相差的月数
     *
     * @param startDate startDate
     * @param endDate endDate
     * @return int
     */
    public static int getTowDateMonth(Date startDate, Date endDate){
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(getSdf(SIMPLESF_PATTERN).parse(getSdf(SIMPLESF_PATTERN).format(startDate)));
            c2.setTime(getSdf(SIMPLESF_PATTERN).parse(getSdf(SIMPLESF_PATTERN).format(endDate)));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        int result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
        return result == 0 ? 1 : Math.abs(result);
    }

    /**
     * 获取两个时间相隔的天数
     *
     * @param startDate startDate
     * @param endDate endDate
     * @return long
     */
    public static Integer getTowDateDay(Date startDate, Date endDate){
        Calendar beginCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        beginCalendar.setTime(startDate);
        endCalendar.setTime(endDate);
        long beginTime = beginCalendar.getTime().getTime();
        long endTime = endCalendar.getTime().getTime();
        return Integer.parseInt(String.valueOf((endTime - beginTime) / (1000 * 60 * 60 *24)));
    }

    /**
     * 获取两个时间相隔的天数
     *
     * @param startDate startDate
     * @param endDate endDate
     * @return long
     */
    public static Integer getTowDateDay(String startDate, String endDate){
        Calendar beginCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        try {
            beginCalendar.setTime(getSdf(ALLSF_PATTERN).parse(startDate));
            endCalendar.setTime(getSdf(ALLSF_PATTERN).parse(endDate));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        long beginTime = beginCalendar.getTime().getTime();
        long endTime = endCalendar.getTime().getTime();
        return Integer.parseInt(String.valueOf((endTime - beginTime) / (1000 * 60 * 60 *24)));
    }


    /**
     * 得到几天前的时间
     * @param d d
     * @param day day
     * @return Date Date
     */
    public static Date getDateBefore(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }

    /**
     * 得到几天后的时间
     * @param d d
     * @param day day
     * @return Date Date
     */
    public static Date getDateAfter(Date d,int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
        return now.getTime();
    }

    /**
     * 获取当天的最小时间 on 2019/5/29 14:54
     *
     * @param time time
     * @return String
     */
    public static String dayMinTime(String time){
        if(time == null || "".equals(time.trim())){
            return time;
        }
        if(time.length() <= 10){
            time = time + " " + "00:00:00";
        }
        return time;
    }

    /**
     * 获取当天的最大时间 on 2019/5/29 14:54
     *
     * @param time time
     * @return String
     */
    public static String dayMaxTime(String time){
        if(time == null || "".equals(time.trim())){
            return time;
        }
        if(time.length() <= 10){
            time = time + " " + "23:59:59";
        }
        return time;
    }
}